// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: "未登录",
      id: "点击此处登录"
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    getApp().request(
      "GET", "/apis/user/me", null,
      function (res) {
        if (res.statusCode == 401) {
          return;
        }
        console.log(res);
        let data = res.data.data;
        that.setData({
          'userInfo.nickName': data.nickName,
          'userInfo.id': data.id,
        })
        if (data.avatar != null) {
          that.setData({
            'userInfo.avatar': "https://www.liahnu.top/picture/uploads/" + data.avatar
          })
        }
      },
      function (res) {
        console.error(res)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onLoginButton: function () {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  onBindWechatButton: function () {
    var that = this;
    let authorization = wx.getStorageSync('Authorization');
    wx.login({
      success: (res) => {
        let code = res.code;
        let token = wx.getStorageSync('Authorization');
        console.log(token)
        console.log(code)
        wx.request({
          url: 'https://www.liahnu.top/apis/user/bindWechat',
          method: "POST",
          header: {
            'Authorization': token,
            'content-type': "application/x-www-form-urlencoded"
          },
          data: {
            code: code
          },
          success: function (res) {
            wx.showToast({
              title: '绑定成功',
              icon: "success"
            })
          }
        })
      },
    })
  },
  
  address: function () {
    wx.navigateTo({
      url: '/pages/loction/loction',
    })
  }
})